package leetcode.oldVersion;

import java.util.HashMap;

public class lc560 {
    public int subarraySum(int[] nums, int k) {
        int count = 0, prefixSum = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, 1); // 鍒濆鎯呭喌锛氬墠缂€鍜屼负0鍑虹幇1娆★紙绌烘暟缁勶級

        for (int num : nums) {
            prefixSum += num; // 璁＄畻褰撳墠鍓嶇紑鍜�
            if (map.containsKey(prefixSum - k)) {
                count += map.get(prefixSum - k); // 鎵惧埌婊¤冻鏉′欢鐨勫瓙鏁扮粍
            }
            map.put(prefixSum, map.getOrDefault(prefixSum, 0) + 1); // 鏇存柊鍝堝笇琛�
        }
        return count;
    }

    public static void main(String[] args) {
        int [] nums1 = {1,1,1};
        int k1 = 2;
        lc560 l = new lc560();
        System.out.println(l.subarraySum(nums1, k1));

        int [] nums2 = {1,2,3};
        int k2 = 3;
        System.out.println(l.subarraySum(nums2, k2));
    }
}
